import { Injectable, EventEmitter } from '@angular/core';

import { UserManageApirequest } from '../ApiRequest/UserManage.Apirequest';
import { usermanegeServiceInterface } from './interface/usermanege.service.interface';
@Injectable()
export class UsermanegeService implements usermanegeServiceInterface {

  stepsEmit: EventEmitter<number> = new EventEmitter;

  constructor(
    private userManageApirequest: UserManageApirequest
  ) { }

  private roleLs: Role[];

  async createUser(UserName: string, Password: string, RoleId: string): Promise<string> {
    return await this.userManageApirequest.createUser(UserName, Password, RoleId);
  }

  /**
   * @param UserId 用户id
   * @param UserName 用户名
   * @param RoleId 角色id
   */
  async updateUser(UserId: string, UserName: string, RoleId: string): Promise<void> {
    return await this.userManageApirequest.updateUser(UserId, UserName, RoleId);
  }

  async deleteUser(UserId: any): Promise<void> {
    return await this.userManageApirequest.deleteUser(UserId);
  }

  async getUserLs(): Promise<User[]> {
    return await this.userManageApirequest.getUserLs();
  }

  async reserUserPwd(UserId: string, NewPassword: string): Promise<void> {
    return this.userManageApirequest.reserUserPwd(UserId, NewPassword);
  }

  async getRoleLs(): Promise<Role[]> {
    if (this.roleLs) return Promise.resolve(this.roleLs);
    this.roleLs = await this.userManageApirequest.getRoleLs();
    return this.roleLs;
  }

  async getUserById(userId: string): Promise<User> {
    return this.userManageApirequest.getUserById(userId);
  }

}

export class User {
  "UserId": string;
  "UserName": string;
  "Role": {
    "RoleId": string,
    "RoleName": string
  }
}

export class Role {
  "RoleId": string;
  "RoleName": string
}